<?php
	require_once("include/class.mysql.php");
	
	class contacts extends mysql {
		function add_contact($first, $last, $nick, $addr, $state, $zip, $city, $country, $day, 
			$month, $year, $notes, $home, $work, $mobile, $mail, $msn, $icq, $ym, $user_group, $web)
		{
			$this->conn();
			$sql = "INSERT INTO " . DB_PREFIX . "contacts (f_name, l_name, nick, address, zipcode, state, city, country, web, mail, icq,
					msn, ym, ph_home, ph_work, ph_mobile, comments, user_group, day, month, year)
					values ('$first', '$last', '$nick', '$addr', '$zip', '$state', '$city',
							'$country', '$web', '$mail', '$icq', '$msn', '$ym', '$home', '$work',
							'$mobile', '$notes', '$user_group', '$day', '$month', '$year')";
			mysql_query($sql) or die(mysql_error());
			$this->close();
		}
		
		function update_contact($id, $first, $last, $nick, $addr, $state, $zip, $city, $country, $day, 
			$month, $year, $notes, $home, $work, $mobile, $mail, $msn, $icq, $ym, $user_group, $web)
		{
			$this->conn();
			$sql = "UPDATE " . DB_PREFIX . "contacts SET f_name = '$first', l_name = '$last', nick = '$nick', address = '$addr',
					zipcode = '$zip', state = '$state', city = '$city', country = '$country', web = '$web', mail = '$mail', icq = '$icq',
					msn = '$msn', ym = '$ym', ph_home = '$home', ph_work = '$work', ph_mobile = '$mobile', comments = '$notes',
					user_group = '$user_group', day = '$day', month = '$month', year = '$year' WHERE id = '$id'";
			mysql_query($sql) or die(mysql_error());
			$this->close();
		}
		
		function rm_contact($id) {
			$this->conn();
			$user_group = $_SESSION['group'];
			$check = mysql_query("SELECT * FROM " . DB_PREFIX . "contacts WHERE id = '$id' AND user_group = '$user_group'") or die(mysql_error());
			$num = mysql_num_rows($check);
			
			if($num == 0) {
				die("Error: Contact does not exist!");
			}
			
			else {
				$sql = "DELETE FROM " . DB_PREFIX . "contacts WHERE id = '$id'";
				mysql_query($sql) or die(mysql_error());
			}
			
			$this->close();
		}
	}
	
	class users extends mysql {
		function rm_user($id) {
			$this->conn();
			$check = mysql_query("SELECT * FROM " . DB_PREFIX . "users WHERE id = '$id'") or die(mysql_error());
			$num = mysql_num_rows($check);
			
			if($num == 0) {
				die("Error: User does not exist?");
			}
			
			else {
				$sql = "DELETE FROM " . DB_PREFIX . "users WHERE id = '$id'";
				mysql_query($sql) or die(mysql_error());
			}
			$this->close();
		}
		
		function add_user($uname, $pword, $group, $level, $check, $group2) {
			$this->conn();
			$sql = "INSERT INTO " . DB_PREFIX . "users (uname, pword, priv, user_group)
					values ('$uname', '$pword', '$level'";
			
			if($check == 1) {
				echo "checkbox was checked<br />";
				$sql .= ", '$group2')";
				
				$sql2 = mysql_query("SELECT * FROM " . DB_PREFIX . "groups WHERE group_name = '$group2'") or die(mysql_error());
				$num = mysql_num_rows($sql2);
				
				echo "[" . $num . "] checking for existing group..<br />";
				
				if($num == 0) {
					echo "we have to make a new group<br />";
					$sql3 = "INSERT INTO " . DB_PREFIX . "groups (group_name)
							values ('$group2')";
					mysql_query($sql3) or die(mysql_error());
				}
			}
			
			else {
				echo "checkbox was not checked<br />";
				if($group == "new") {
					$groupp = $uname;
				}
				else {
					$groupp = $group;
				}
				$sql .= ", '$groupp')";
				$sql2 = mysql_query("SELECT * FROM " . DB_PREFIX . "groups WHERE group_name = '$groupp'") or die(mysql_error());
				$num = mysql_num_rows($sql2);
				
				if($num == 0) {
					$sql3 = "INSERT INTO " . DB_PREFIX . "groups (group_name)
							values ('$groupp')";
					mysql_query($sql3) or die(mysql_error());
				}
			}
			mysql_query($sql) or die(mysql_error());
			echo "I'm done ;)";
			$this->close();
		}
		
		function update_user($id, $uname, $pword, $group, $level, $check, $group2) {
			$this->conn();
			$sql = "UPDATE " . DB_PREFIX . "users SET uname = '$uname',";
			if(!empty($pword)) {
				$sql .= " pword = '$pword',";
			}
			
			$sql .= " priv = '$level', user_group = '";
			
			if($check == 1) {
				echo "checkbox was checked<br />";
				$sql .= "$group2'";
				$_SESSION['group'] = $group2;
				
				$sql2 = mysql_query("SELECT * FROM " . DB_PREFIX . "groups WHERE group_name = '$group2'") or die(mysql_error());
				$num = mysql_num_rows($sql2);
				
				echo "[" . $num . "] checking for existing group..<br />";
				
				if($num == 0) {
					echo "we have to make a new group<br />";
					$sql3 = "INSERT INTO " . DB_PREFIX . "groups (group_name)
							values ('$group2')";
					mysql_query($sql3) or die(mysql_error());
				}
			}
			
			else {
				echo "checkbox was not checked<br />";
				if($group == "new") {
					$groupp = $uname;
				}
				else {
					$groupp = $group;
				}
				$sql .= "$groupp'";
				$_SESSION['group'] = $groupp;
				$sql2 = mysql_query("SELECT * FROM " . DB_PREFIX . "groups WHERE group_name = '$groupp'") or die(mysql_error());
				$num = mysql_num_rows($sql2);
				
				if($num == 0) {
					$sql3 = "INSERT INTO " . DB_PREFIX . "groups (group_name)
							values ('$groupp')";
					mysql_query($sql3) or die(mysql_error());
				}
			}
			$sql .= " WHERE id = '$id'";
			mysql_query($sql) or die(mysql_error());
			echo "I'm done ;)";
			$this->close();
		}
	}
	
	class groups extends mysql {
		function add_group($group) {
			$this->conn();
			$sele = mysql_query("SELECT * FROM " . DB_PREFIX . "groups WHERE group_name = '$group'") or die(mysql_error());
			$num2 = mysql_num_rows($sele);
			
			if($num2 >= 1) {
				echo "group already exists";
				exit;
			}
			
			$sql = "INSERT INTO " . DB_PREFIX . "groups (group_name)
					VALUES ('$group')";
					
			mysql_query($sql) or die(mysql_error());
			$this->close();
		}
		
		function remove($id) {
			$this->conn();
			// Get group name
			$sql = mysql_query("SELECT group_name FROM " . DB_PREFIX . "groups WHERE id = '$id'") or die(mysql_error());
			$g_name = mysql_fetch_row($sql) or die(mysql_error());
			$g_name = $g_name[0];
			
			// Delete users
			$sql2 = "DELETE FROM " . DB_PREFIX . "users WHERE user_group = '$g_name'";
			
			// Delete contacts
			$sql3 = "DELETE FROM " . DB_PREFIX . "contacts WHERE user_group = '$g_name'";
			
			// Delete group
			$sql4 = "DELETE FROM " . DB_PREFIX . "groups WHERE group_name = '$g_name' AND id = '$id'";
			
			// Execute it
			mysql_query($sql2) or die(mysql_error());
			mysql_query($sql3) or die(mysql_error());
			mysql_query($sql4) or die(mysql_error());
			$this->close();
		}
	}
	
	class params extends mysql {
		function get_level($uid) {
			if(!empty($uid) && is_numeric($uid)) {
				$this->conn();
				$sql = mysql_query("SELECT priv FROM " . DB_PREFIX . "users WHERE id = '$uid'") or die(mysql_error());
				$get = mysql_fetch_row($sql) or die(mysql_error());
				$get = $get[0];
				$this->close();
				return $get;
			}
		}
	}
	
	$params = new params;
	$contacts = new contacts;
	$users = new users;
	$groups = new groups;
?>